$(document).ready(function() 
{
	$('.forMediaHTML').focus(function(){
		activeTextAreaId = this.id;
	});

	var mediaBlock = $('#mediaBlockFilter');
	
	mediaBlock.find('form').submit(function(){
	    $(this).ajaxSubmit({
        	dataType: 'json',
            success: function(data){
						HandleResponseJSON(data, function(data){
							LoadMediaFilesBlock(data);
						});
					}
        });
	    return false;		
	});
	
	mediaBlock.find('.reset').click(function(){
		mediaBlock.find('form :input:visible:not(:submit,:reset)').val('');	
		
		$.ajax({url: this.href,
			dataType: 'json',
			type: 'get',
			success: function(data){
						HandleResponseJSON(data, function(data){
							LoadMediaFilesBlock(data);
						});
					}
		});
		
		return false;
	});	

});

function LoadMediaFilesBlock(data)
{
	$('#mediaBlockFiles').html(data);
}

function addMediaToContent(mediaId)
{
	var textArea = $('#'+activeTextAreaId);
	if (!textArea) return false;
					
    $.getJSON(
			'<?=htmlspecialchars(Mozg::Router('http')->Create('module=admin&ctrl=media&action=get_media_content'));?>',
			{"id": mediaId},
			function(data){
				HandleResponseJSON(data, function(mediaHTML){
					if (CKEDITOR.instances[activeTextAreaId])
					{	
						CKEDITOR.instances[activeTextAreaId].insertHtml( mediaHTML );
					}			
					else if (document.getElementById(activeTextAreaId))	
					{	
						insertAtCursor(document.getElementById(activeTextAreaId), mediaHTML);
					}
				});	
			}
		);			

	return false;
};	